package com.start.boot.web;


import com.start.boot.common.SystemConfiguration;
import com.start.boot.domain.Param_CaseInfo;
import com.start.boot.service.CaseInfoService;
import com.start.boot.support.utils.EasyUIHelper;
import com.start.boot.support.utils.FileUtils;
import com.start.boot.utils.WebServiceUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/8.
 */
@RestController
@RequestMapping("/caseinfo")
public class CaseInfoController extends ArchivesSystemBaseController {

    @Autowired
    private CaseInfoService caseInfoService;
    @Autowired
    private WebServiceUtils webServiceUtils;

    /**
     * 获取关联案件列表
     *
     * @param pcslbm
     * @param bmsah
     * @return
     */
    @RequestMapping("/getGlajs")
    public String getGlajs(String pcslbm, String bmsah) {
        String result = "";

        try {
            List<Map> list = caseInfoService.getGlajs(pcslbm, bmsah, "");
            result = EasyUIHelper.buildTreeDataSourceWithoutIconCol(list, "BMSAH", "FBM", "AJLBMC", "-1");
        } catch (Exception e) {
            super.errMsg("获取关联案件列表失败。", pcslbm + ";" + bmsah, e);
        }

        return result;
    }

    /**
     * 获取文书卷宗文件列表
     *
     * @param pcslbm
     * @param bmsah
     * @return
     */
    @RequestMapping("/getDocFiles")
    public String getDocFiles(String pcslbm, String bmsah) {
        String result = "";

        try {
            List<Map> list = caseInfoService.getDocFiles(pcslbm, bmsah, "");
            result = EasyUIHelper.buildTreeDataSourceWithoutIconCol(list, "ID", "PID", "NAME", "-1");
        } catch (Exception e) {
            super.errMsg("获取文书卷宗文件列表失败", pcslbm + ";" + bmsah, e);
        }

        return result;
    }

    /**
     * 获取电子卷宗文件列表
     *
     * @param pcslbm
     * @param bmsah
     * @return
     */
    @RequestMapping("/getDossierFiles")
    public String getDossierFiles(String pcslbm, String bmsah) {
        String result = "";

        try {
            List<Map> list = caseInfoService.getDossierFiles(pcslbm, bmsah, "");
            result = EasyUIHelper.buildTreeDataSourceWithoutIconCol(list, "ID", "PID", "NAME", "-1");
        } catch (Exception e) {
            super.errMsg("获取电子卷宗文件列表失败", pcslbm + ";" + bmsah, e);
        }

        return result;
    }

    /**
     * 获取本地文件配置
     *
     * @return
     */
    @RequestMapping("/getLocalFilePath")
    public String getLocalFilePath() {

        return "/LocalFile/";
    }

    /**
     * 获取文书卷宗文件
     *
     * @param pcslbm
     * @param bmsah
     * @param wsbh
     * @param wjlj
     * @param wjlx
     * @return
     */
    @RequestMapping("/getDocFile")
    public String getDocFile(String pcslbm, String bmsah, String wsbh, String wjlj, String wjlx, String sfzpdf) {
        String result = "";

        try {

            // 调用WebService/GetDocmentFile获取文书卷宗文件
            Map params = new HashMap();
            params.put("pcslbm", pcslbm == null ? "" : pcslbm);
            params.put("bmsah", bmsah);
            params.put("wsbh", wsbh);
            params.put("filePath", wjlj);
            params.put("wjlx", wjlx);
            //params.put("isConvertToPDF", SystemConfiguration.wssfzpdf);
            params.put("isConvertToPDF", sfzpdf.equalsIgnoreCase("Y") ? "true" : "false");
            Object data = webServiceUtils.post(SystemConfiguration.webservice + "/GetDocmentFile", params);
            if(data == null){
                throw new Exception("调用WebService获取文书卷宗文件失败，返回为空。");
            }

            // 解析XML文档
            List<String> list = webServiceUtils.parseXml(data.toString());
            if(StringUtils.isNoneEmpty(list.get(0))){
                throw new Exception("调用WebService获取文书卷宗文件失败，" + list.get(0));
            }

            // 文书卷宗文件路径
            String filePath = SystemConfiguration.wsjzPath + list.get(1);
            if (!(SystemConfiguration.fwwsPath + list.get(1)).equals(SystemConfiguration.wzbsPath + filePath)) {
                FileUtils.copyFile(new File(SystemConfiguration.fwwsPath + list.get(1)), new File(SystemConfiguration.wzbsPath + filePath));
            }
            result = success(filePath, "获取文书卷宗文件成功。");

        } catch (Exception e) {
            super.errMsg("获取文书卷宗文件失败。", pcslbm + ";" + bmsah + ";" + wsbh + ";" + wjlj, e);
            result = failure(e.getMessage(), "获取文书卷宗文件失败。");
        }

        return result;
    }

    /**
     * 获取电子卷宗文件
     *
     * @param pcslbm
     * @param dwbm
     * @param bmsah
     * @param jzbh
     * @return
     */
    @RequestMapping("/getDossierFile")
    public String getDossierFile(String pcslbm, String dwbm, String bmsah, String jzbh) {
        String result = "";

        try {

            // 调用WebService/GetDossierFile获取电子卷宗文件
            Map params = new HashMap();
            params.put("bmsah", bmsah);
            params.put("cbdwbm", dwbm);
            params.put("bm", jzbh);
            params.put("pcslbm", pcslbm);
            Object data = webServiceUtils.post(SystemConfiguration.webservice + "/GetDossierFile", params);
            if(data == null){
                throw new Exception("调用WebService获取电子卷宗文件失败，返回为空。");
            }

            // 解析XML文档
            List<String> list = webServiceUtils.parseXml(data.toString());
            if(StringUtils.isNoneEmpty(list.get(0))){
                throw new Exception("调用WebService获取电子卷宗文件失败，" + list.get(0));
            }

            // 电子卷宗文件路径
            String filePath = SystemConfiguration.dzjzPath + list.get(1);
            if (!(SystemConfiguration.fwjzPath + list.get(1)).equals(SystemConfiguration.wzbsPath + filePath)) {
                FileUtils.copyFile(new File(SystemConfiguration.fwjzPath + list.get(1)), new File(SystemConfiguration.wzbsPath + filePath));
            }
            result = success(filePath, "获取电子卷宗文件成功。");

        } catch (Exception e) {
            super.errMsg("获取电子卷宗失败。", pcslbm + ";" + jzbh, e);
            result = failure(e.getMessage(), "获取电子卷宗文件失败。");
        }

        return result;
    }

    /**
     * 获取案卡配置
     * @param pcslbm
     * @param ajlbbm
     * @param bmsah
     * @return
     */
    @RequestMapping("/getCaseCards")
    public String getCaseCards(String pcslbm, String ajlbbm, String bmsah) {
        String result = "";

        try {

            // 调用WebService/GetCaseCards获取案卡配置项
            Map params = new HashMap();
            params.put("pcslbm", pcslbm);
            params.put("bmsah", bmsah);
            params.put("ajlbbm", ajlbbm);
            Object data = webServiceUtils.post(SystemConfiguration.webservice + "/GetCaseCards", params);
            if(data == null){
                throw new Exception("调用WebService获取案卡配置项失败，返回为空。");
            }

            // 解析XML文档
            List<String> list = webServiceUtils.parseXml(data.toString());
            if(StringUtils.isNoneEmpty(list.get(0))){
                throw new Exception("调用WebService获取案卡配置项失败，" + list.get(0));
            }

            // 案卡项配置
            result = list.get(1);

        } catch (Exception e) {
            super.errMsg("获取案卡配置失败。", pcslbm + ";" + bmsah, e);
        }

        return result;
    }

    /**
     * 获取过程项配置
     * @param pcslbm
     * @param ajlbbm
     * @param bmsah
     * @return
     */
    @RequestMapping("/getProcessCards")
    public String getProcessCards(String pcslbm, String ajlbbm, String bmsah) {
        String result = "";

        try {

            // 调用WebService/GetProcessCards获取过程项配置项
            Map params = new HashMap();
            params.put("pcslbm", pcslbm);
            params.put("bmsah", bmsah);
            params.put("ajlbbm", ajlbbm);
            Object data = webServiceUtils.post(SystemConfiguration.webservice + "/GetProcessCards", params);
            if(data == null){
                throw new Exception("调用WebService获取过程项配置项失败，返回为空。");
            }

            // 解析XML文档
            List<String> list = webServiceUtils.parseXml(data.toString());
            if(StringUtils.isNoneEmpty(list.get(0))){
                throw new Exception("调用WebService获取过程项配置项失败，" + list.get(0));
            }

            // 过程项项配置
            result = list.get(1);
        } catch (Exception e) {
            super.errMsg("获取过程项配置失败。", pcslbm + ";" + bmsah, e);
        }

        return result;
    }

    /**
     * 获取案卡项信息
     *
     * @param pcslbm
     * @param bmsah
     * @param ajlbbm
     * @param akname
     * @return
     */
    @RequestMapping("/getCaseCardInfo")
    public String getCaseCardInfo(String pcslbm, String bmsah, String ajlbbm, String akname) {
        String result = "";

        try {
            // 调用WebService/GetCaseCardInfo获取过程项配置项
            Map params = new HashMap();
            params.put("pcslbm", pcslbm);
            params.put("bmsah", bmsah);
            params.put("ajlbbm", ajlbbm);
            params.put("akname", akname);
            Object data = webServiceUtils.post(SystemConfiguration.webservice + "/GetCaseCardInfo", params);
            if(data == null){
                throw new Exception("调用WebService获取案卡项信息失败，返回为空。");
            }

            // 解析XML文档
            List<String> list = webServiceUtils.parseXml(data.toString());
            if(StringUtils.isNoneEmpty(list.get(0))){
                throw new Exception("调用WebService获取案卡项信息失败，" + list.get(0));
            }

            // 案卡项信息
            result = success(list.get(1), "获取案卡项信息成功。");
        } catch (Exception e) {
            super.errMsg("获取案卡项信息失败。", bmsah + ";" + akname, e);
        }

        return result;
    }

    /**
     * 获取过程项信息
     *
     * @param pcslbm
     * @param bmsah
     * @param ajlbbm
     * @param gcakxh
     * @return
     */
    @RequestMapping("/getProcessCardInfo")
    public String getProcessCardInfo(String pcslbm, String bmsah, String ajlbbm, String gcakxh) {
        String result = "";

        try {
            // 调用WebService/GetProcessCardInfo获取过程项配置项
            Map params = new HashMap();
            params.put("pcslbm", pcslbm);
            params.put("bmsah", bmsah);
            params.put("ajlbbm", ajlbbm);
            params.put("akname", gcakxh);
            Object data = webServiceUtils.post(SystemConfiguration.webservice + "/GetProcessCardInfo", params);
            if(data == null){
                throw new Exception("调用WebService获取过程项信息失败，返回为空。");
            }

            // 解析XML文档
            List<String> list = webServiceUtils.parseXml(data.toString());
            if(StringUtils.isNoneEmpty(list.get(0))){
                throw new Exception("调用WebService获取过程项信息失败，" + list.get(0));
            }

            // 过程项信息
            result = success(list.get(1), "获取过程项信息成功。");
        } catch (Exception e) {
            super.errMsg("获取过程项信息失败。", bmsah + ";" + gcakxh, e);
        }

        return result;
    }

    /**
     * 获取案件基本信息
     *
     * @param bmsah
     * @return
     */
    @RequestMapping("/getCaseInfo")
    public String getCaseInfo(String pcslbm, String bmsah) {
        String result = "";

        try {
            Param_CaseInfo caseInfo = caseInfoService.getCaseInfo(pcslbm, bmsah);
            if(caseInfo == null || StringUtils.isEmpty(caseInfo.BMSAH)){
                throw new Exception("获取到案件信息为空。");
            }

            String html = FileUtils.readFile(new File(SystemConfiguration.wzbsPath + "/view/CaseDetailInfo.htm"));
            html = html.replaceAll("@BMSAH", filterNull(caseInfo.BMSAH)).replaceAll("@TYSAH", filterNull(caseInfo.TYSAH));
            html = html.replaceAll("@CBDW", filterNull(caseInfo.CBDW_MC)).replaceAll("@SLRQ", filterNull(caseInfo.SLRQ));
            html = html.replaceAll("@AJMC", filterNull(caseInfo.AJMC)).replaceAll("@AJLB", filterNull(caseInfo.AJLB_MC));
            html = html.replaceAll("@CBBM", filterNull(caseInfo.CBBM_MC)).replaceAll("@CBR", filterNull(caseInfo.CBR));
            html = html.replaceAll("@ZCJG", filterNull(caseInfo.ZCJG_DWMC)).replaceAll("@YSDW", filterNull(caseInfo.YSDW_DWMC));
            html = html.replaceAll("@YSWSWH", filterNull(caseInfo.YSWSWH)).replaceAll("@YSAY", filterNull(caseInfo.YSAY_AYMC));
            html = html.replaceAll("@YSQTAY", filterNull(caseInfo.YSQTAY_AYMCS)).replaceAll("@SWAJ", filterNull(caseInfo.SFSWAJ));
            html = html.replaceAll("@DBAJ", filterNull(caseInfo.SFDBAJ)).replaceAll("@GZAJ", filterNull(caseInfo.SFGZAJ));
            html = html.replaceAll("@BLTS", filterNull(caseInfo.BLTS)).replaceAll("@WCRQ", filterNull(caseInfo.WCRQ));
            html = html.replaceAll("@GDRQ", filterNull(caseInfo.GDRQ)).replaceAll("@GDR", filterNull(caseInfo.GDR));
            html = html.replaceAll("@AQZY", filterNull(caseInfo.AQZY));

            // 过程项信息
            result = success(html, "获取案件基本信息成功。");
        } catch (Exception e) {
            super.errMsg("获取案件基本信息失败。", bmsah, e);
            result = failure(e.getMessage(), "获取案件基本信息失败。");
        }

        return result;
    }


    private static String filterNull(String value){
        return value == null ? "" : value;
    }
}
